import QtQuick 2.12
import QtQuick.Controls 1.4
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.12
import QtLocation 5.12
import QtQuick.Window 2.12
import QtGraphicalEffects 1.0
import AIClient.module 1.0
import Result.module 1.0
import QtQuick.Dialogs 1.3

Window {
    modality: Qt.ApplicationModal
    title: "修改密码"
    id: reset_dlg
    visible: false
    height: Screen.desktopAvailableHeight*0.3
    width: Screen.desktopAvailableWidth*0.3

    property string m_email:""
    Image {
        anchors.fill: parent
        source: "qrc:/images/images/AIback.jpg"
    }
    TipMessage {
        id: tip
        width: parent.width
    }

    AIClient {
        id: client
    }

    function reset()
    {
        fieldPasswdOld.text = ""
        filedPawd.text = ""
        filedPawd_accept.text = ""
    }

    //用户输入
    Rectangle {
        width: 250
        height: 180
        color: "transparent"
        anchors.centerIn: parent

//                    anchors.bottom: parent.bottom
//                    anchors.bottomMargin: 100
//                    anchors.horizontalCenter: parent.horizontalCenter

        GridLayout {
            id: grid_reset
            rows: 4
            columns: 2
            anchors.fill: parent
            columnSpacing: 10
//            Text {
//                Layout.column: 0
//                Layout.row: 0
//                text: qsTr("邮箱")
//                font.pointSize: 12
//                font.bold: true
//                Layout.minimumHeight: 20
//                Layout.maximumHeight: 50
//                color: "orange"
//            }
//            TextField {
//                Layout.column: 1
//                Layout.row: 0
//                id: filedUsr
//                font.pointSize: 12
//                Layout.minimumHeight: 20
//                Layout.maximumHeight: 50
//                Layout.preferredWidth: 200
//                text: "superadmin@aiplatform.com"
//                Layout.preferredHeight: 40
//                background: Rectangle{
//                    color: "white"
//                    anchors.fill: filedUsr
//                }
//            }

            Text {
                Layout.column: 0
                Layout.row: 0
                text: qsTr("旧密码")
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                font.pointSize: 12
                font.bold: true
                color: "orange"
            }




            TextField {

                id: fieldPasswdOld
                Layout.column: 1
                Layout.row: 0
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                Layout.preferredWidth: 200
                Layout.preferredHeight: 50
                //font.pointSize: 12
                //anchors.fill: parent
                echoMode: "Password"
                passwordCharacter: "*"
                text: ""
                selectByMouse: true
                background: Rectangle{
                    color: "white"
                    anchors.fill: fieldPasswdOld
                }
            }


            Text {
                Layout.column: 0
                Layout.row: 1
                text: qsTr("输入密码")
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                font.pointSize: 12
                font.bold: true
                color: "orange"
            }

            TextField {
                Layout.column: 1
                Layout.row: 1
                id: filedPawd
                //font.pointSize: 12
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                Layout.preferredWidth: 200
                echoMode: "Password"
                passwordCharacter: "*"
                text: ""
                Layout.preferredHeight: 50
                selectByMouse: true
                background: Rectangle{
                    color: "white"
                    anchors.fill: filedPawd
                }
            }

            Text {
                Layout.column: 0
                Layout.row: 2
                text: qsTr("确认密码")
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                font.pointSize: 12
                font.bold: true
                color: "orange"
            }

            TextField {
                Layout.column: 1
                Layout.row: 2
                id: filedPawd_accept
                //font.pointSize: 12
                Layout.minimumHeight: 20
                Layout.maximumHeight: 50
                Layout.preferredWidth: 200
                echoMode: "Password"
                passwordCharacter: "*"
                text: ""
                Layout.preferredHeight: 50
                selectByMouse: true
                background: Rectangle{
                    color: "white"
                    anchors.fill: filedPawd_accept
                }
            }

            //登录按钮
            Rectangle {
                id: logBtn
                width: 150
                height: 33
                color: "#6facf9"
                radius: 4
                border.width: 1
                border.color: "black"
                Layout.column: 1
                Layout.row: 3
                Layout.fillWidth: true
                Text {
                    color: "#ffffff"
                    text: qsTr("提交")
                    anchors.centerIn: parent
                    font.pointSize: 12
                }
                Layout.alignment: Qt.AlignVCenter | Qt.AlignHCenter
//                            anchors.horizontalCenter: parent.horizontalCenter
//                            anchors.top: grid.bottom
//                            anchors.topMargin: 40

                MouseArea {
                    anchors.fill: parent
                    hoverEnabled: true
                    onEntered: {
                        logBtn.color = "#333333"
                    }
                    onPressed: {
                        logBtn.color = "#111111"
                        if ( filedPawd.text.length <= 5 )
                        {
                            tip.showmsg("密码长度不得少于6！")
                            return
                        }

                        if ( filedPawd_accept.text.length <= 5 )
                        {
                            tip.showmsg("密码长度不得少于6！")
                            return
                        }



                        if ( fieldPasswdOld.text.length <= 0 )
                        {
                            tip.showmsg("验证码不得为空！")
                            return
                        }


                        if ( filedPawd.text !== filedPawd_accept.text )
                        {
                            tip.showmsg("密码输入不一致！")
                            return
                        }

                        //var res = "{\"code\":0}"
                        //client.setConfig("")
                        var res = client.reset_password_by_auth(m_email,fieldPasswdOld.text,filedPawd.text)
                        var jres = JSON.parse(res)
                        if (jres["code"] === 0) {
                            tip.showmsg("修改密码成功")
                            delay(2000)

                        }
                        else
                        {
                            tip.showmsg(jres["message"])
                        }

                        //window.m_token = jres["token"]
                        //console.info(window.m_token)
                    }
                    onReleased: {
                        logBtn.color = "#333333"
                    }

                    onExited: {
                        logBtn.color = "#6facf9"
                    }
                }
            }
        }


    }

    Timer {
        id: timer
    }
    function delay(delayTime) {
        //var timer = new Timer();
        timer.interval = delayTime;
        timer.repeat = false;
        timer.triggered.connect(function () {
            reset_dlg.hide()
        })
        timer.start();
    }

}
